package review.data.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import review.config.Constants;
import review.entity.Title;
import review.entity.model.TitleModel;
import review.entity.model.TitleTreeModel;
import review.data.service.TitleService;

@Controller
@RequestMapping("/syatem")
@Api(tags = { "职称操作接口" })
public class TitleAction {

  @Autowired
  private TitleService titleService;

  @ApiOperation(value = "添加职称", notes = "", httpMethod = "POST")
  @RequestMapping(value = "/add_Title", produces = "application/json;charset=UTF-8")
  @ResponseBody
  public Object add(TitleModel data) {
    JSONObject obj = new JSONObject();
    try {
      titleService.addTitle(data);
      obj.put(Constants.SUCCESS, true);
    } catch (Exception e) {
      obj.put(Constants.MESSAGE, e.getMessage());
      obj.put(Constants.SUCCESS, false);
    }
    return obj;
  }

  @ApiOperation(value = "删除职称", notes = "", httpMethod = "POST")
  @RequestMapping(value = "/delete_Title", produces = "application/json;charset=UTF-8")
  @ResponseBody
  public Object delete(String name) {
    JSONObject obj = new JSONObject();
    try {
      titleService.deleteTitle(name);
      obj.put(Constants.SUCCESS, true);
    } catch (Exception e) {
      obj.put(Constants.MESSAGE, e.getMessage());
      obj.put(Constants.SUCCESS, false);
    }
    return obj;
  }

  @ApiOperation(value = "修改职称", notes = "", httpMethod = "POST")
  @RequestMapping(value = "/update_Title", produces = "application/json;charset=UTF-8")
  @ResponseBody
  public Object update(TitleModel data) {
    JSONObject obj = new JSONObject();
    try {
      titleService.updateTitle(data);
      obj.put(Constants.SUCCESS, true);
    } catch (Exception e) {
      obj.put(Constants.MESSAGE, e.getMessage());
      obj.put(Constants.SUCCESS, false);
    }
    return obj;
  }

  @ApiOperation(value = "分页查询职称", notes = "", httpMethod = "GET")
  @RequestMapping(value = "/query_Title", produces = "application/json;charset=UTF-8")
  @ResponseBody
  public Object query(Title title, @RequestParam(defaultValue = "1") int page,
      @RequestParam(defaultValue = "10") int size) {
    JSONObject obj = new JSONObject();
    try {
      Page<TitleModel> titleModels = titleService.findAll(page, size, title);
      obj.put(Constants.SUCCESS, true);
      obj.put(Constants.DATA, titleModels);
    } catch (Exception e) {
      obj.put(Constants.MESSAGE, e.getMessage());
      obj.put(Constants.SUCCESS, false);
    }
    return obj;
  }

  @ApiOperation(value = "查询所有职称（树形结构）", notes = "", httpMethod = "GET")
  @RequestMapping(value = "/title_model", produces = "application/json;charset=UTF-8")
  @ResponseBody
  public Object queryAll() {
    JSONArray array = new JSONArray();
    try {
      List<TitleTreeModel> models = titleService.findAllModel();
      for (TitleTreeModel titleTreeModel : models) {
        array.add(titleTreeModel.toJSON());
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return array;
  }

}
